package edu.hawaii.duedates.userlibraryinfo;


import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import edu.hawaii.duedates.book.Book;
import edu.hawaii.duedates.library.LibraryType;
import edu.hawaii.duedates.user.User;

/**
 * This will test the UserLibraryInfo class.
 * 
 * @author Ronn Reeves and Robin E. Raqueno
 *
 */
public class TestUserLibraryInfo {
  
  /**
   * Test constructor of the UserLibraryInfo class.
   */
  @Test
  public void testCreateULI() {
    String lib = "library";
    List<Book> bookList = new ArrayList<Book>();
    List<User> user1 = new ArrayList<User>();
    List<LibraryType> library = new ArrayList<LibraryType>();
    LibraryType libType = new LibraryType();
    List<Boolean> validities = new ArrayList<Boolean>();
    
    user1.add(new User("User1", "password1"));
    libType.setName("Kaiser");
    library.add(libType);
    UserLibraryInfo uli = new UserLibraryInfo();
    uli.setBooks(bookList);
    uli.setLibraries(library);
    uli.setUsers(user1);
    uli.setValidities(validities);
    
    assertEquals("toString test failed.", uli.toString(), "Kaiser - User1\n");
    assertEquals("getBooks() test failed.", uli.getBooks().size(), 0);
    assertEquals("getLibraries() test failed.", uli.getLibraries(), library);
    assertEquals("getUsers() test failed.", uli.getUsers(), user1);
    assertEquals("getValidities() test failed.", uli.getValidities(), validities);
    assertEquals("getReport() test failed.", uli.getReport(), "");
    
    User user2 = new User("John", "password"); 
    uli.add(user2, libType);
    assertEquals("add() test failed.", uli.getUsers().size(), 2);

    //Test display()
    UserLibraryInfo userLI = new UserLibraryInfo();
    List<Book> bookList2 = new ArrayList<Book>();
    Book book1 = new Book("Title1", "10/11/2008", "Hamilton");
    Book book2 = new Book("Title2", "10/01/2008", "Aiea Library");
    bookList2.add(book1);
    bookList2.add(book2);
    userLI.setBooks(bookList2);
    userLI.createReport(lib, 0);
    userLI.createReport("duedate", -1);
    bookList2.remove(book1);
    bookList2.remove(book2);
    userLI.createReport(lib, 0);
    bookList2.add(book2);
    bookList2.add(book1);
    userLI.createReport(lib, 10);
  }
  
  
  /**
   * Tests bookSort.
   */
  @Test
  public void testBookSort() {
    UserLibraryInfo uli = new UserLibraryInfo();

    List<LibraryType> library = new ArrayList<LibraryType>();
    LibraryType libType1 = new LibraryType();
    libType1.setName("Hamilton");
    library.add(libType1);
    LibraryType libType2 = new LibraryType();
    libType2.setName("Aiea Library");
    library.add(libType2);

    List<Book> bookList = new ArrayList<Book>();
    Book book1 = new Book("Title1", "10/11/2008", "Hamilton");
    Book book2 = new Book("Title2", "10/01/2008", "Aiea Library");
    bookList.add(book1);
    bookList.add(book2);
    uli.setBooks(bookList);
    
    //Test sorting
    List<Book> sortedBooks = uli.bookSort("library");
    
    String errorMsg = "bookSort() failed.";
    
    assertEquals(errorMsg, bookList.get(1), sortedBooks.get(0));
    assertEquals(errorMsg, bookList.get(0), sortedBooks.get(1));
    
    sortedBooks = uli.bookSort("duedate");
    assertEquals(errorMsg, bookList.get(1), sortedBooks.get(0));
    
    bookList.remove(1);
    bookList.remove(0);
    uli.bookSort("duedate");
    
  }
}
